#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include "rfs-client.h"
int numread;

int
rls(PACKAGE *package) {

    /* Write to the pipe */
    package->op = RLS;
    write(package->to, package, sizeof (PACKAGE));

    char rdbuf[DATA_LENGTH];
    memset(rdbuf, '\0', DATA_LENGTH);

    char * output = (char *) malloc(1);
    output[0] = '\0';

    numread = 0;
    do {
        read(package->from, package, sizeof (PACKAGE));
        output = (char *) realloc(output, strlen(output) + strlen(package->data));
        strcat(output, package->data);
    } while (package->offset == DATA_LENGTH);

    printf("Resultado\n");
    printf("-----------------\n");
    printf("%s\n", output);
    printf("-----------------\n");
    return (package->block_count - 1)*DATA_LENGTH + package->offset;
}

int
rcd(PACKAGE *package) {

    /* Write to the pipe */

    package->op = RCD;

    write(package->to, package, sizeof (PACKAGE));

    char rdbuf[DATA_LENGTH];
    memset(rdbuf, '\0', DATA_LENGTH);

    char * output = (char *) malloc(1);
    output[0] = '\0';

    numread = 0;
    do {
        read(package->from, package, sizeof (PACKAGE));
        output = realloc(output, strlen(output) + strlen(package->data));
        strcat(output, package->data);
    } while (package->op == DATA_LENGTH);

    printf("Resultado");
    printf("-----------------\n");
    printf("%s\n", output);
    printf("-----------------\n");
    return (package->block_count - 1)*DATA_LENGTH + package->op;
}

int
get(PACKAGE *package) {

    int block_size = 0;
    FILE * file = NULL;

    char * file_name = (char *) malloc(DATA_LENGTH);
    strncpy(file_name, package->data, DATA_LENGTH);
    package->block_count = 0;
    do {
        package->op = GET;
        strncpy(package->data, file_name, DATA_LENGTH);
        write(package->to, package, sizeof (PACKAGE));
        block_size = read(package->from, package, sizeof (PACKAGE));

        if (package->op != ERROR) {
            if (file == NULL)
                file = fopen(file_name, "w");
            // escribo el bloque del lado del cliente
            printf("-----------------\n");
            printf("===> %i",fwrite(package->data, sizeof (char), package->offset, file));
            printf("-----------------\n");
            // preparo la llamada siguiente
            package->block_count++;
            // traigo el siguiente bloque            
        } else {
            continue;
        }
    } while (package->offset == DATA_LENGTH);
    printf("-----------------\n");
    if (package->op != ERROR) {

        printf("Resultado:\nSe descargó correctamente el archivo '%s'.\n"
                "Se transmitieron %i bytes en total.\n",
                file_name,
                (package->block_count - 1) * DATA_LENGTH + package->offset);
    } else {
        printf("Ocurrió un error al intentar descargar el archivo.\n"
                "El archivo '%s' no existe o el servidor no tiene permiso para leerlo.\n",
                file_name);
    }
    printf("-----------------\n");
    if (file != NULL)
        // cierro el archivo
        fclose(file);

    free(file_name);
    return package->op;
}

int
put(PACKAGE *package) {
    static int clnt_res;

    memset((char *) & clnt_res, 0, sizeof (clnt_res));
    return (clnt_res);
}

